package com.favccxx.ticketrule.api;

import com.favccxx.ticketrule.application.TicketService;
import com.favccxx.ticketrule.domain.ticket.entity.OutDoorTicket;
import com.favccxx.ticketrule.domain.ticket.entity.Tourist;
import io.swagger.annotations.ApiOperation;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("/ticket")
public class SaleTicketController {

    @Autowired
    private KieContainer kieContainer;
    @Autowired
    private TicketService ticketService;

    @ApiOperation(value = "一个测试API", notes = "第一个测试api")
    @PostMapping("/doctorSale")
    public ResponseEntity<String> sale(@RequestParam(value = "ticketType", defaultValue = "3A") String ticketType,
                                       @RequestParam(value = "ticketName", defaultValue = "") String ticketName,
                                       @RequestParam(value = "userName", defaultValue = "") String userName,
                                       @RequestParam(value = "age", defaultValue = "") int age) {
        Tourist tourist = new Tourist();
        tourist.setName(userName);
        tourist.setAge(age);

        OutDoorTicket outDoorTicket = new OutDoorTicket();
        outDoorTicket.setType(ticketType);
        outDoorTicket.setName(ticketName);
        outDoorTicket.setPrice(150D);

        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(outDoorTicket);
        kieSession.fireAllRules(); // fire all rules defined into drool file (drl)
        kieSession.dispose();

        Double price = ticketService.costPrice(outDoorTicket, tourist);

        return new ResponseEntity<String>(String.valueOf(price), HttpStatus.OK);
    }


}
